package com.example.bilibili;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.example.bilibili.home.FragmentA;
import com.example.bilibili.home.FragmentB;
import com.example.bilibili.utils.MediaControllerListener;
import com.example.bilibili.utils.MediaView;

public class GoodsInfoActivity extends FragmentActivity implements View.OnClickListener, MediaControllerListener {

    private RadioGroup rgMain2;
    private RadioButton rbJianjie;
    private RadioButton rbComment;

    private com.example.bilibili.utils.MediaView videoView;
    private LinearLayout content;
    private RelativeLayout parentview;
    private LinearLayout parentview1;
    private GoodsBean goodsBean;

    private FragmentA f_a;
    private FragmentB f_b;

    private Fragment[] mFragments;
    private int mIndex;



    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2021-02-09 13:10:25 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    private void findViews() {
        rgMain2 = (RadioGroup)findViewById( R.id.rg_main2 );
        rbJianjie = (RadioButton)findViewById( R.id.rb_jianjie );
        rbComment = (RadioButton)findViewById( R.id.rb_comment );
        videoView = (MediaView) findViewById( R.id.videoView );
        content = (LinearLayout) findViewById( R.id.content );
        parentview1 = (LinearLayout) findViewById( R.id.parentview1 );
        parentview = (RelativeLayout) findViewById( R.id.parentview );


        rbJianjie.setOnClickListener( this );
        rbComment.setOnClickListener( this );
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);
        findViews();
        initView();
        initFragment();

        //接收数据
        goodsBean = (GoodsBean) getIntent().getSerializableExtra("GOODS_BEAN");
        if (goodsBean != null) {
            setDataForView(goodsBean);
        }

    }

    private void initFragment() {
        f_a =new FragmentA();
        f_b =new FragmentB();

        rgMain2.check(R.id.rb_jianjie);

        //添加到数组
        mFragments = new Fragment[]{f_a,f_b};
        //开启事务
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft= fragmentManager.beginTransaction();
        //添加首页
        ft.add(R.id.content,f_a).commit();
        //默认设置为第0个
        setIndexSelected(0);

    }
    //方法一，选中显示与隐藏
    //方法一，选中显示与隐藏
    private void setIndexSelected(int index) {

        goodsBean = (GoodsBean) getIntent().getSerializableExtra("GOODS_BEAN");
        if(index==0){

            FragmentA fragmenta = new FragmentA();
            Bundle bundlea = new Bundle();
            bundlea.putString("touxiang",Constants.BASE_IMAGE_URL+goodsBean.getTouxiang());
            bundlea.putString("id",goodsBean.getId());
            bundlea.putString("fensi",goodsBean.getFensi());
            bundlea.putString("bofangliang",goodsBean.getBofangliang());
            bundlea.putString("pinglun",goodsBean.getCommennt());
            bundlea.putString("shijian",goodsBean.getShijian());
            bundlea.putString("dianzan",goodsBean.getDianzan());
            bundlea.putString("toubi",goodsBean.getToubi());
            bundlea.putString("shoucang",goodsBean.getShoucang());
            bundlea.putString("zhuanfa",goodsBean.getZhuanfa());


            fragmenta.setArguments(bundlea);//数据传递到fragment中
            FragmentManager fragmentManagera = getSupportFragmentManager();
            FragmentTransaction fragmentTransactiona = fragmentManagera.beginTransaction();
            fragmentTransactiona.replace(R.id.content,fragmenta);
            fragmentTransactiona.commit();
        }else{
            FragmentB fragmentb = new FragmentB();
            Bundle bundleb = new Bundle();
            bundleb.putString("c1",Constants.BASE_IMAGE_URL+goodsBean.getC1());
            bundleb.putString("c2",Constants.BASE_IMAGE_URL+goodsBean.getC2());
            bundleb.putString("c3",Constants.BASE_IMAGE_URL+goodsBean.getC3());
            fragmentb.setArguments(bundleb);//数据传递到fragment中
            FragmentManager fragmentManagerb = getSupportFragmentManager();
            FragmentTransaction fragmentTransactionb = fragmentManagerb.beginTransaction();
            fragmentTransactionb.replace(R.id.content,fragmentb);
            fragmentTransactionb.commit();

        }

    }

    private void initView() {


        rgMain2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int arg1) {
                //遍历RadioGroup 里面所有的子控件。
                for (int index = 0; index < group.getChildCount(); index++) {
                    //获取到指定位置的RadioButton
                    RadioButton rb = (RadioButton)group.getChildAt(index);
                    //如果被选中
                    if (rb.isChecked()) {
                        setIndexSelected(index);
                        //tIndexSelectedTwo(index);  //方法二
                        break;
                    }
                }

            }
        });

    }


    private void setDataForView(GoodsBean goodsBean) {
        String path = Constants.BASE_IMAGE_URL+goodsBean.getShipin();

        videoView.setFullScreenListener(this);
        content = (LinearLayout) findViewById(R.id.content);
        videoView.start(path);
        rbComment.setText("评论"+goodsBean.getCommennt());
    }

    @Override
    public void isFullScreen() {
        content.setVisibility(View.GONE);
        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) parentview.getLayoutParams();
        linearParams.height=1080;
        parentview.setLayoutParams(linearParams);

    }

    @Override
    public void isLittleScreen() {
        content.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) parentview.getLayoutParams();
        linearParams.height=800;
        parentview.setLayoutParams(linearParams);
    }


    @Override
    protected void onPause() {
        super.onPause();
        videoView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.onDestroy();
    }


    @Override
    public void onClick(View v) {
        if ( v == rbJianjie ) {
            Toast.makeText(this, "简介", Toast.LENGTH_SHORT).show();
        } else if ( v == rbComment ) {
            Toast.makeText(this, "评论", Toast.LENGTH_SHORT).show();
        }
    }

}